﻿using System;
using System.Threading;

namespace Cms.Web.UI.Admin.HelperClasses
{
    public class DatePickerHelper
    {
        #region DatePickerSetup class declaration

        private class DatePickerSetup
        {
            public readonly string ampm;
            public readonly string closeText;
            public readonly string currentText;
            public readonly string datePattern;
            public readonly string dayNamesMin;

            public readonly string firstDay,
                                   hourText,
                                   minuteText;

            public readonly string monthNames;

            public readonly string nextText;
            public readonly string prevText;

            public readonly string showWeek;
            public readonly string timePattern;
            public readonly string timeText;
            public readonly string weekHeader;

            public DatePickerSetup(string locale)
            {
                datePattern = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;
                timePattern = Thread.CurrentThread.CurrentUICulture.DateTimeFormat.ShortTimePattern;
                firstDay = ((int)Thread.CurrentThread.CurrentUICulture.DateTimeFormat.FirstDayOfWeek).ToString();
                // Convert the .NET format to jquery datepicker format
                datePattern = datePattern.ToLower().Replace("yyyy", "yy");
                timePattern = timePattern.Replace("tt", "TT").Replace("t", "T").Replace("H", "h");
                if (timePattern.Contains("T") && !timePattern.Contains("TT"))
                    timePattern = timePattern.Replace("T", "TT");
                ampm = timePattern.Contains("TT") ? "true" : "false";
                switch (locale)
                {
                    case "sv-SE":
                        hourText = "Timme";
                        minuteText = "Minut";
                        currentText = "Nu";
                        closeText = "Klar";
                        timeText = "Tid";
                        prevText = "Föregående";
                        nextText = "Nästa";
                        monthNames =
                            "'Januari', 'Februari', 'Mars', 'April', 'Maj', 'Juni', 'Juli', 'Augusti', 'September', 'Oktober', 'November', 'December'";
                        dayNamesMin = "'Sö', 'Må', 'Ti', 'On', 'To', 'Fr', 'Lö'";
                        showWeek = "true";
                        weekHeader = "v";
                        break;
                    default:
                        hourText = "Hour";
                        minuteText = "Minute";
                        currentText = "Now";
                        closeText = "Done";
                        timeText = "Time";
                        prevText = "Previous";
                        nextText = "Next";
                        monthNames =
                            "'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'";
                        dayNamesMin = "'Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'";
                        showWeek = "true";
                        weekHeader = "w";
                        break;
                }
            }
        }

        #endregion

        public static string GetDatePickerSetup(string locale)
        {
            var setup = new DatePickerSetup(locale);

            return "function setupDatePicker(txt) {" +
                   "$(txt).datetimepicker({" +
                   GetCommonDeclarations(setup) +
                   "});" +
                   "}";
        }

        public static string GetInlineDatePickerSetup(string locale, bool useMinDate)
        {
            var setup = new DatePickerSetup(locale);
            string declaration = "function setupInlineDatePicker(div, hid) {" +
                                 "$(div).datetimepicker({" +
                                 GetCommonDeclarations(setup) +
                                 ", onSelect: function (dateText, inst) { $(hid).val(dateText); }";
            if (useMinDate)
                declaration += ", minDate: cms.date.getRoundedDate(new Date(), 'up', 'm', 5)";
            declaration += "});" + Environment.NewLine;
            declaration += "}";
            return declaration;
        }
        
        private static string GetCommonDeclarations(DatePickerSetup setup)
        {
            return "firstDay: " + setup.firstDay + ", " +
                   "dateFormat: '" + setup.datePattern + "', " +
                   "timeFormat: '" + setup.timePattern + "', " +
                   "ampm: " + setup.ampm + ", " +
                   "hourText: '" + setup.hourText + "', " +
                   "minuteText: '" + setup.minuteText + "', " +
                   "currentText: '" + setup.currentText + "', " +
                   "closeText: '" + setup.closeText + "', " +
                   "timeText: '" + setup.timeText + "', " +
                   "prevText: '" + setup.prevText + "', " +
                   "nextText: '" + setup.nextText + "', " +
                   "stepHour: 1, " +
                   "stepMinute: 5, " +
                   "monthNames: [" + setup.monthNames + "], " +
                   "dayNamesMin: [" + setup.dayNamesMin + "], " +
                   "showWeek: " + setup.showWeek + ", " +
                   "weekHeader: '" + setup.weekHeader + "'";
        }
    }
}